// src/hooks/useLoading.tsx
import { useEffect, useRef } from "react";
import { Toast } from "antd-mobile";
import { ToastHandler } from "antd-mobile/es/components/toast";
import { SpinLoading } from "antd-mobile";

export default function useLoading(isLoading: boolean, isSuccess: boolean) {
    // 用于保存加载中提示的引用对象
    const toastHandler = useRef<ToastHandler>();
    // 监控是否正在获取用户个人资料
    useEffect(() => {
        // 如果正在获取
        if (isLoading) {
            // 弹出"加载中"的用户提示并保存其引用对象
            toastHandler.current = Toast.show({
                content: <SpinLoading />,
                duration: 0,
            });
        }
    }, [isLoading]);

    // 监控获取用户个人资料是否成功
    useEffect(() => {
        // 如果成功, 关闭"加载中"的用户提示
        if (isSuccess) toastHandler.current?.close();
        // 如果组件卸载
        return () => {
            // 关闭"加载中"的用户提示
            toastHandler.current?.close();
        };
    }, [isSuccess]);

    return toastHandler;
}